<html>
<head><meta charset="utf-8"><title>Ordering::Consume · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ordering.3A.3AConsume.html">Ordering::Consume</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="216680855"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ordering%3A%3AConsume/near/216680855" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ordering.3A.3AConsume.html#216680855">(Nov 13 2020 at 22:10)</a>:</h4>
<p>So, I get why Ordering::Unordered doesn't exist — it's not part of the memory model we snarfed from C11. <code>Consume</code> is though, so it seems like a strange omission.</p>
<p>It is definitely the most... subtle of the memory orderings, but it's essentially free even on platforms like ARM, and without it you're forced to use Acquire, which is definitely not free.</p>



<a name="216683915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ordering%3A%3AConsume/near/216683915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ordering.3A.3AConsume.html#216683915">(Nov 13 2020 at 22:43)</a>:</h4>
<p>Seems like it'd be useful for things like exposing shared memory or memmaps without being UB.  Not necessarily _useful_ if it's actually changing unexpectedly, but at least not UB.</p>



<a name="216686701"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ordering%3A%3AConsume/near/216686701" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ordering.3A.3AConsume.html#216686701">(Nov 13 2020 at 23:09)</a>:</h4>
<p>It's useful whenever you need pointer or thing from which a pointer is derived (for example, an index) in an atomic.</p>
<ul>
<li>
<p>In <a href="https://github.com/rust-lang/rust/blob/master/library/std/src/sys/unix/weak.rs#L42-L67">https://github.com/rust-lang/rust/blob/master/library/std/src/sys/unix/weak.rs#L42-L67</a> I have a WIP patch that removes the SeqCst but isn't able to make it as cheap as it could be on arm (lots of comments explaining why): <a href="https://gist.github.com/thomcc/0005c6f2f36a142dcba0bbb69ed33d4a">https://gist.github.com/thomcc/0005c6f2f36a142dcba0bbb69ed33d4a</a> — if i could use Consume on that load i wouldn't need the fence.</p>
</li>
<li>
<p>I suspect some of the Relaxed loads we have currently should be Consume, for example: <a href="https://github.com/rust-lang/rust/blob/master/library/std/src/sys_common/thread_local_key.rs#L145">https://github.com/rust-lang/rust/blob/master/library/std/src/sys_common/thread_local_key.rs#L145</a></p>
</li>
</ul>



<a name="216687066"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ordering%3A%3AConsume/near/216687066" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ordering.3A.3AConsume.html#216687066">(Nov 13 2020 at 23:15)</a>:</h4>
<p>I believe that there are some foundational issues with <code>memory_order_consume</code> in C11 semantics that make it not actually work for its intended purpose</p>



<a name="216687193"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ordering%3A%3AConsume/near/216687193" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ordering.3A.3AConsume.html#216687193">(Nov 13 2020 at 23:16)</a>:</h4>
<p>it's often called subtle in the sense "only a few people in the world understand it" but I think it's closer to "it is broken but the model is so complex that only a few people are willing to stick their neck out and claim this directly"</p>



<a name="216687367"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ordering%3A%3AConsume/near/216687367" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ordering.3A.3AConsume.html#216687367">(Nov 13 2020 at 23:19)</a>:</h4>
<p>Yeah, that's fair. I certainly can't claim to understand it as well as the other orderings. It's unfortunate that without it you need acquire for the common pattern of "I loaded this pointer from an atomic and will read from it", though.</p>



<a name="216687469"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ordering%3A%3AConsume/near/216687469" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ordering.3A.3AConsume.html#216687469">(Nov 13 2020 at 23:20)</a>:</h4>
<p>if you really care about avoiding the fence on ARM I think you can just use <code>Ordering::Relaxed</code> (which is wrong) and just gate on the processor being ARM, or use volatile and/or <code>asm!</code> because the memory model isn't working for you</p>



<a name="216687576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ordering%3A%3AConsume/near/216687576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ordering.3A.3AConsume.html#216687576">(Nov 13 2020 at 23:22)</a>:</h4>
<p>I mean, I don't think that would get r+</p>



<a name="216687714"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ordering%3A%3AConsume/near/216687714" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ordering.3A.3AConsume.html#216687714">(Nov 13 2020 at 23:23)</a>:</h4>
<p>IIRC there is a good discussion of the problems with <code>memory_order_consume</code> in the <a href="https://people.mpi-sws.org/~viktor/papers/popl2017-promising.pdf">promising semantics</a> paper. Basically, what counts as a "data dependency" is well defined at the architectural level but is not stable under basic optimizations like <code>x + 0 ~&gt; x</code> so it is not really appropriate for a high level language</p>



<a name="216689144"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ordering%3A%3AConsume/near/216689144" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ordering.3A.3AConsume.html#216689144">(Nov 13 2020 at 23:39)</a>:</h4>
<p>Yeah, it's just annoying. On 32 bit arm a fence(Acquire) forces <code>dmb ish</code> to be emitted, which is basically a <code>fence(SeqCst)</code>. On 64 bit arm it is a <code>dmb ishld</code> which is at least an improvement. The C11 semantics are likely flawed but they do give you the tools to avoid this sort of thing.</p>



<a name="216690259"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ordering%3A%3AConsume/near/216690259" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ordering.3A.3AConsume.html#216690259">(Nov 13 2020 at 23:51)</a>:</h4>
<p>I guess LLVM doesn't even support consume though, :(</p>



<a name="216696908"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ordering%3A%3AConsume/near/216696908" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ordering.3A.3AConsume.html#216696908">(Nov 14 2020 at 01:37)</a>:</h4>
<blockquote>
<p>I believe that there are some foundational issues with <code>memory_order_consume</code> in C11 semantics that make it not actually work for its intended purpose</p>
</blockquote>
<p>Supporting this, C++17 deprecated it because of issues in those semantics. I think 20 fixed some, but AFAIK it's still deprecated.</p>



<a name="216726171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ordering%3A%3AConsume/near/216726171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ordering.3A.3AConsume.html#216726171">(Nov 14 2020 at 12:25)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/213817-t-lang/topic/Ordering.3A.3AConsume/near/216687714">said</a>:</p>
<blockquote>
<p>IIRC there is a good discussion of the problems with <code>memory_order_consume</code> in the <a href="https://people.mpi-sws.org/~viktor/papers/popl2017-promising.pdf">promising semantics</a> paper. Basically, what counts as a "data dependency" is well defined at the architectural level but is not stable under basic optimizations like <code>x + 0 ~&gt; x</code> so it is not really appropriate for a high level language</p>
</blockquote>
<p>Agreed. <code>Consume</code> is ill-defined currently; even more ill-defined than <code>Relaxed</code> (and that one already has enough problems to worry me^^ the keyword here is "out-of-thin-air").</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>